/*
 * Created by JFormDesigner on Tue Mar 29 18:58:20 CST 2022
 */

package com.wangcanxuan.www.view.tribe;

import java.awt.event.*;
import javax.swing.table.*;

import com.wangcanxuan.www.controller.Impl.TaskControllerImpl;
import com.wangcanxuan.www.controller.Impl.TribeControllerImpl;
import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.TaskController;
import com.wangcanxuan.www.controller.TribeController;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.Task;
import com.wangcanxuan.www.entity.po.Tribe;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;
import com.wangcanxuan.www.util.LoggerUtil;
import com.wangcanxuan.www.util.SecurityUtil;
import com.wangcanxuan.www.util.ValidateUtil;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 发布任务界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class PublishTaskView extends JFrame {
    private final User user;
    private int level = 1;
    private String chooseTaskName;
    private final TaskController taskController;
    private final TribeController tribeController;
    private final UserController userController;

    public PublishTaskView(User user) {
        this.user = user;
        tribeController = ControllerProxy.getProxyInstance(TribeControllerImpl.class);
        userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);
        taskController = ControllerProxy.getProxyInstance(TaskControllerImpl.class);
        initComponents();
        fillApplyTable();
        setVisible(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    private void backToTribe(ActionEvent e) {
        // TODO add your code here
        ResultInfo<User> refresh = userController.refresh(user.getId());
        ResultInfo<Tribe> tribeResultMessage = tribeController.selectTribeByTribeId(user.getTribeId());
        new TribalInterfaceView(refresh.getObj(), tribeResultMessage.getObj());
        dispose();
    }

    private void publishTask(ActionEvent e) {
        // TODO add your code here
        String taskName = taskNameText.getText();
        if (ValidateUtil.strIsNullOrEmpty(taskName)) {
            JOptionPane.showMessageDialog(null, "任务名不能为空");
            return;
        }

        String taskContent = taskContentText.getText();
        if (ValidateUtil.strIsNullOrEmpty(taskContent)) {
            JOptionPane.showMessageDialog(null, "任务内容不能为空");
            return;
        }
        ResultInfo<?> resultMessage = taskController.publishTask(taskName, taskContent, level, user);
        int code = resultMessage.getCode();
        String message = resultMessage.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new PublishTaskView(user);
            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new PublishTaskView(user);
            dispose();
        }
    }

    private void levelBoxItemStateChanged(ItemEvent e) {
        // TODO add your code here
        if (e.getStateChange() == ItemEvent.SELECTED) {
            level = levelBox.getSelectedIndex() + 1;
        }
    }

    private void shelvesTask(ActionEvent e) {
        // TODO add your code here
        ResultInfo<?> resultMessage = taskController.shelvesTask(chooseTaskName, user.getTribeId());
        int code = resultMessage.getCode();
        String message = resultMessage.getMessage();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new PublishTaskView(user);
            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
        }
    }

    private void taskListTableMousePressed(MouseEvent e) {
        // TODO add your code here
        int selectedRow = taskListTable.getSelectedRow();
        chooseTaskName = String.valueOf(taskListTable.getValueAt(selectedRow, 0));
        chooseNameText.setText(chooseTaskName);
    }

    private void fillApplyTable() {
        ResultInfo<List<Task>> listResultMessage = taskController.selectTaskListByUser(user);
        List<Task> tasks = listResultMessage.getObj();
        DefaultTableModel dtm = (DefaultTableModel) taskListTable.getModel();
        // 设置成0行
        dtm.setRowCount(0);
        if (tasks == null) {
            return;
        }
        for (Task task : tasks) {
            Vector<Object> list = new Vector<>();
            list.add(task.getTaskName());
            list.add(task.getContent());
            if (!ValidateUtil.strIsNullOrEmpty(task.getAcceptorId())) {
                ResultInfo<User> refresh = userController.refresh(task.getAcceptorId());
                list.add(refresh.getObj().getNickName());
                String countdownTime = SecurityUtil.getCountdownTime(task.getAcceptDate());
                if ("".equals(countdownTime)) {
                    list.add("该任务已被完成，可下架任务！");
                } else {
                    list.add(countdownTime);
                }
            } else {
                list.add("该任务未被接受");
                list.add("任务未进行");
            }
            LoggerUtil.getLogger().severe(list + "");
            dtm.addRow(list);
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        backToTribeButton = new JButton();
        label1 = new JLabel();
        label2 = new JLabel();
        taskNameText = new JTextField();
        label3 = new JLabel();
        taskContentText = new JTextField();
        publishTask = new JButton();
        levelBox = new JComboBox<>();
        label4 = new JLabel();
        scrollPane1 = new JScrollPane();
        taskListTable = new JTable();
        label5 = new JLabel();
        label6 = new JLabel();
        chooseNameText = new JTextField();
        shelvesTaskButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();

        //---- backToTribeButton ----
        backToTribeButton.setText("\u8fd4\u56de\u90e8\u843d");
        backToTribeButton.addActionListener(e -> backToTribe(e));

        //---- label1 ----
        label1.setText("\u53d1\u5e03\u4efb\u52a1");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));

        //---- label2 ----
        label2.setText("\u4efb\u52a1\u540d\uff1a");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));

        //---- label3 ----
        label3.setText("\u4efb\u52a1\u5185\u5bb9\uff1a");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));

        //---- publishTask ----
        publishTask.setText("\u53d1\u5e03\u4efb\u52a1");
        publishTask.addActionListener(e -> publishTask(e));

        //---- levelBox ----
        levelBox.setModel(new DefaultComboBoxModel<>(new String[]{
                "1",
                "2",
                "3",
                "4",
                "5",
                "6",
                "7",
                "8",
                "9"
        }));
        levelBox.addItemListener(e -> levelBoxItemStateChanged(e));

        //---- label4 ----
        label4.setText("\u4efb\u52a1\u96be\u5ea6\uff1a");
        label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));

        //======== scrollPane1 ========
        {

            //---- taskListTable ----
            taskListTable.setModel(new DefaultTableModel(
                    new Object[][]{
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                    },
                    new String[]{
                            "\u4efb\u52a1\u540d", "\u4efb\u52a1\u5185\u5bb9", "\u63a5\u53d7\u8005", "\u4efb\u52a1\u5b8c\u6210\u5ea6"
                    }
            ));
            taskListTable.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    taskListTableMousePressed(e);
                }
            });
            scrollPane1.setViewportView(taskListTable);
        }

        //---- label5 ----
        label5.setText("\u5df2\u53d1\u5e03\u7684\u4efb\u52a1");
        label5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 18));

        //---- label6 ----
        label6.setText("\u9009\u4e2d\u4efb\u52a1\uff1a");
        label6.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));

        //---- shelvesTaskButton ----
        shelvesTaskButton.setText("\u4e0b\u67b6\u4efb\u52a1");
        shelvesTaskButton.addActionListener(e -> shelvesTask(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(backToTribeButton, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
                                                .addGap(35, 35, 35)
                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
                                                .addContainerGap(677, Short.MAX_VALUE))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addComponent(label3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE))
                                                                .addGap(30, 30, 30)
                                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                                        .addComponent(taskNameText, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(taskContentText, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(levelBox, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE)))
                                                        .addComponent(publishTask, GroupLayout.PREFERRED_SIZE, 266, GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 132, Short.MAX_VALUE)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                                                .addComponent(label5, GroupLayout.PREFERRED_SIZE, 174, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(189, 189, 189))
                                                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 449, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(56, 56, 56))
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addComponent(label6, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(38, 38, 38)
                                                                .addComponent(chooseNameText, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(59, 59, 59)
                                                                .addComponent(shelvesTaskButton, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
                                                                .addContainerGap())))))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(33, 33, 33)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(backToTribeButton, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                        .addComponent(taskNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE))
                                                .addGap(25, 25, 25)
                                                .addGroup(contentPaneLayout.createParallelGroup()
                                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(taskContentText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                                                .addGap(30, 30, 30)
                                                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                                                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(levelBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)))
                                        .addComponent(label5, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
                                .addGap(55, 55, 55)
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(publishTask, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(chooseNameText, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                                                .addComponent(label6, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                                                .addComponent(shelvesTaskButton, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)))
                                .addContainerGap(110, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JButton backToTribeButton;
    private JLabel label1;
    private JLabel label2;
    private JTextField taskNameText;
    private JLabel label3;
    private JTextField taskContentText;
    private JButton publishTask;
    private JComboBox<String> levelBox;
    private JLabel label4;
    private JScrollPane scrollPane1;
    private JTable taskListTable;
    private JLabel label5;
    private JLabel label6;
    private JTextField chooseNameText;
    private JButton shelvesTaskButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
